//
//  TodayViewController.m
//  Today
//
//  Created by J on 16/6/18.
//  Copyright © 2016年 J. All rights reserved.
//

#import "TodayViewController.h"
#import <UIKit/UIKit.h>
#import "Masonry.h"
#import <NotificationCenter/NotificationCenter.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "TodayKit.h"
@interface TodayViewController () <NCWidgetProviding>
@property(nonatomic,strong)UILabel* label;
@property(nonatomic,strong)NSArray* dataArr;
@property(nonatomic,copy)NSString* str;


@end

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.preferredContentSize = CGSizeMake(0, 200);

    
//    UITableView* tableView= [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];
//    tableView.delegate = self;
//    tableView.dataSource = self;
//    [self.view addSubview:tableView];
//
//    
//      self.str =   [NSString stringWithContentsOfURL:containerURL1 encoding:NSUTF8StringEncoding error:nil];//读取group区沙盒文件
//    self.dataArr = arr;
//
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    [button setBackgroundImage:[UIImage imageNamed:@"rotation"] forState:UIControlStateNormal];
//    button.backgroundColor = [UIColor blackColor];
    button.frame = CGRectMake(100, 0, 100, 100);
    [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
//
//    [sharedDefaults setObject:@"0.7" forKey:@"slider"];
//    [sharedDefaults synchronize];
    
//    NSExtensionItem *imageItem = [self.extensionContext.inputItems firstObject];
//    
//    if(!imageItem){
//        
//        return;
//        
//    }
//    
//    NSItemProvider *imageItemProvider = [[imageItem attachments] firstObject];
//    
//    if(!imageItemProvider){
//        
//        return;
//        
//    }
//    
//    // 检查是否包含文本
//    
//    if([imageItemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]){
//        
//        [imageItemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
//            
//            if(image){
//                
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    
//                    self.imageView.image = image;
//                    
//                }); 
//                
//            } 
//            
//        }]; 
//        
//        
//        
//    } 
    
    
//    [sharedDefaults setObject:self.imageView.image forKey:@"image"];
    NSString* str = [sharedDefaults valueForKeyPath:@"image"];
    self.label.text = str;
    
    
    //app group路径
    
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:shareKey];
    
    NSLog(@"app group:\n%@",containerURL.path);
    
    
    
    //打印可执行文件路径
    
    NSLog(@"bundle:\n%@",[[NSBundle mainBundle] bundlePath]);
    
    
    
    //打印documents
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *path = [paths objectAtIndex:0];
    
    NSLog(@"documents:\n%@",path);
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
//    cell.textLabel.text = @(indexPath.row).description;
//    cell.textLabel.text = self.str;
    
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.str]];
//        UIImage* image= [UIImage imageWithData:data];
//        if (data != nil) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                //在这里做UI操作(UI操作都要放在主线程中执行)
//                cell.imageView.image = image;
//                
//            });
//        }
//    });
    
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.str]];
    UIImage* image= [UIImage imageWithData:data];    
    cell.imageView.image = image;
    return cell;
}


 
- (void)btn:(UIButton*)sender{
    [self.extensionContext openURL:[NSURL URLWithString:@"simpleTimer://finished"] completionHandler:^(BOOL success) {

        
    }];
}



- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
    return UIEdgeInsetsZero;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    // Perform any setup necessary in order to update the view.
    
    // If an error is encountered, use NCUpdateResultFailed
    // If there's no update required, use NCUpdateResultNoData
    // If there's an update, use NCUpdateResultNewData

    completionHandler(NCUpdateResultNewData);
}





- (void)setHasContent:(BOOL)flag forWidgetWithBundleIdentifier:(NSString *)bundleID{
    
}



@end
